﻿--[[
   @brief    GameAction
   @author   xiaokunyang
   @version  1.0
   @date     2015-03-17
--]]

ierd_tgp.tpf_ui.Log("loading game_action.lua");

ierd_tgp.tpf_ui.Require("common\\common.lua");

CloudGameConfigMask = {
    CGC_MASK_SWITCH = 1;
    CGC_MASK_TCLS_CONFIG = 2;
    CGC_MASK_PLUGIN_ID = 4;
};

GameActions = {
    kUnkown = 0;
    kDownloadGame = 1;           -- 下载游戏
    kStartGame = 2;              -- 开始游戏
    kUpdateGame = 3;             -- 更新游戏
    kUpdatePlugin = 4;           -- 更新助手
    kInstallGame = 5;            -- 安装游戏
    
    kPauseDownload = 101;        -- 暂停游戏下载
    kPauseUpdate = 103;          -- 暂停游戏更新
    kPauseUpdatePlugin = 104;    -- 暂停助手更新
        
    kCancelDownload = 201;       -- 取消下载
    kStopGame = 202;             -- 停止游戏
    kCancelUpdate = 203;         -- 取消更新游戏
    kCancelUpdatePlugin = 204;   -- 取消更新插件
    
    kContinueDownload = 401;     -- 继续游戏下载
    kContinueUpdate = 403;       -- 继续游戏更新 
    kContinueUpdatePlugin = 404; -- 继续助手更新
};

WGGameAction = {
    act2fun_ = {};
};

RailGameActionInfo = {
};

function WGGameAction.init()
    WGGameAction.act2fun_[GameActions.kDownloadGame] = WGGameAction.download;
    WGGameAction.act2fun_[GameActions.kStartGame] = WGGameAction.launch;
    WGGameAction.act2fun_[GameActions.kUpdateGame] = WGGameAction.update;
    WGGameAction.act2fun_[GameActions.kUpdatePlugin] = WGGameAction.update_plugin;
    WGGameAction.act2fun_[GameActions.kInstallGame] = WGGameAction.install;
    
    WGGameAction.act2fun_[GameActions.kPauseDownload] = WGGameAction.pause_download;
    WGGameAction.act2fun_[GameActions.kPauseUpdate] = WGGameAction.pause_update;
    WGGameAction.act2fun_[GameActions.kPauseUpdatePlugin] = WGGameAction.pause_update_plugin;
    
    WGGameAction.act2fun_[GameActions.kContinueDownload] = WGGameAction.continue_download;
    
    WGGameAction.act2fun_[GameActions.kCancelDownload] = WGGameAction.cancel_download;
    WGGameAction.act2fun_[GameActions.kStopGame] = WGGameAction.stop_game;
    WGGameAction.act2fun_[GameActions.kCancelUpdate] = WGGameAction.cancel_update;
    WGGameAction.act2fun_[GameActions.kCancelUpdatePlugin] = WGGameAction.cancel_update_plugin;

    --WGGameAction.act2fun_[GameActions.kContinueUpdate] = WGGameAction.continue_update;
end

function WGGameAction.on_broadcast_excute_action(data)
    if data == nil then
        base_utility.log("[e][Banner]on_excute_update, invalid data: " .. tostring(data), true);
        return;
    end
    WGGameAction.on_excute_action(event_id, base_utility.table_to_json(data))
end

function WGGameAction.on_excute_action(event_id, json_param)
    if nil ~= json_param then
        base_utility.log("[Banner]on_excute_action, json_str: " .. json_param, true);
        local json_data = base_utility.json_to_table(json_param);
        local action = json_data.action;
        local action_info = json_data.action_info;
        if nil == action or nil == action_info then
            base_utility.log("[E][Banner]on_excute_action, invalid param: " .. json_param, true);
            return;
        end
        WGGameAction.excute_action(action, base_utility.json_to_table(action_info));
    end
end

function WGGameAction.excute_action(action, action_info)
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    if game_lib == nil then
        base_utility.log(string.format("excute_game_act, game_lib is NIL, game_id_:%d", action_info.game_id_));
        return false;
    end;

    local game_channel = game_lib:get_game_distribution_channels(action_info.game_id_);

    if action == GameActions.kStartGame then
        if WGGameAction.IsFilterGameInAccLaunchCfg(action_info.game_id_) then
            WGGameAction.PopTipsForSwitchQQLogin();
            return;
        end;

        local data_out = comm_center.call_service("Svr_IsCloudGameRunning",
            {game_id = action_info.game_id_});
        -- 有云游戏在launching或running状态，都弹框提示
        if data_out ~= nil and (data_out.is_running or data_out.is_launching) then
            base_utility.log(
                string.format("excute_game_act, cloud game_id_:%d running", action_info.game_id_));
            local json_tbl = {};
            json_tbl.game_id = action_info.game_id_;
            local json_str = base_utility.table_to_json(json_tbl);
            event_center.send_event(
                popup_wnd_event.SHOW_CLOUD_GAME_ALREADY_RUNNING_TIPS, json_str);
            return;
        end
    end

    if action ~= GameActions.kStartGame and action ~= GameActions.kStopGame then
        local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                        {game_id = action_info.game_id_});
        if data_out ~= nil and not data_out.bAllowContinue then
            return;
        end
    end

    
    -- 网吧版本单机下载/更新弹提示
    if banner_model.distribution_channels.CHANNELS_RAIL == game_channel and 
       (action == GameActions.kDownloadGame or action == GameActions.kUpdateGame) then
        local show_netbar_tips = true;
        if action_info ~= nil and action_info.show_netbar_tips ~= nil then
            show_netbar_tips = action_info.show_netbar_tips;
        end
        if show_netbar_tips and base_utility.is_netbar_ver() then
            local desc_text = "";
            local ok_text = "";
            local cancel_text = "";
            if action == GameActions.kDownloadGame then
                desc_text = LoadStr("NETBAR_INSTALL_TIPS");
                ok_text = LoadStr("NETBAR_INSTALL_OK");
                cancel_text = LoadStr("NETBAR_INSTALL_CANCEL");
            elseif action == GameActions.kUpdateGame then
                desc_text = LoadStr("NETBAR_UPDATE_TIPS");
                ok_text = LoadStr("NETBAR_UPDATE_OK");
                cancel_text = LoadStr("NETBAR_UPDATE_CANCEL");
            end;

            if desc_text ~= "" and ok_text ~= "" and cancel_text ~= "" then
                local param = {
                    position = 'center',--参数值: rbottom-右下角，center-居中
                    title = LoadStr("POP_TIPS_TIPS"),
                    width = 480,
                    height = 230,
                    titleIcon = "",
                    icon = "",
                    tipsIcon = poptips_icons.kWarning,
                    descTitle = "",
                    desc = desc_text,
                    is_multi_win = true,
                    cancelBtn = {
                        visible = true,
                        close_win = true,
                        text = cancel_text,
                        handle = function()
                        end
                    },
                    okBtn = {
                        visible = true,
                        close_win = true,
                        text = ok_text,
                        handle = function()
                            action_info = action_info or {};
                            action_info.show_netbar_tips = false;
                            WGGameAction.excute_action(action, action_info);
                        end
                    }
                }

                poptips_control.show_tips(param);
                return true;      
            else
                base_utility.log("[Banner][game_action]no text, " .. tostring(desc_text) .. ", "
                    .. tostring(ok_text) .. ", " .. tostring(cancel_text), true);
            end
        end;
    end

    if action == GameActions.kDownloadGame then
        -- 游戏没有配置下载链接，不支持下载，或处于封/内测阶段
        local game_detail_info = ierd_tgp.game_library.GameDetailInfo:new_local();
        game_lib:get_game_detail_info(action_info.game_id_, game_detail_info);
        local cur_operate_phase_class = "";
        local game_main_type = nil;
        if game_detail_info ~= nil then
            cur_operate_phase_class = game_detail_info.operate_phase_class;
            game_main_type = game_detail_info.main_type;
            base_utility.log("[Banner][game_action]game_id: " .. action_info.game_id_ 
                .. ", cur_game_operate_phase_class " .. cur_operate_phase_class);
        end
        if game_main_type ~= nil then
            if type(game_main_type) == "string" then
                game_main_type = tonumber(game_main_type);
            end
            if game_main_type == 1 then
                local is_exist_download_url = game_lib:IsExistDownloadUrl(action_info.game_id_);
                if is_exist_download_url ~= nil and is_exist_download_url == false then
                    popup_wnd_model.show_operate_state_wnd();
                    base_utility.log("[Banner][game_action]game_id: " .. action_info.game_id_ 
                        .. ", download url is empty");
                    return;
                end
            end
        end

        -- 查询端游白名单
        if game_main_type ~= nil and game_main_type == 1 then 
            base_utility.log("[Banner][game_action]download game_id: " .. action_info.game_id_ 
                .. ", query mmog game whitelist");
            if nil ~= game_lib then
                game_lib:GetWhiteListStatus(action_info.game_id_,
                    "game_library_control.on_get_mmog_game_release_status");
            end;
            return false;
        end
    end

    -- 判断是否为发行平台的游戏，如果是则需要查询游戏权限信息
    -- 单机游戏启动不在此处查询权限，在game_library中查询
    if banner_model.distribution_channels.CHANNELS_RAIL == game_channel then
        base_utility.log(string.format("excute_game_act, game is rail, game_id_:%d", action_info.game_id_));
        if action == GameActions.kDownloadGame then
            RailGameActionInfo[tostring(action_info.game_id_)] = action_info;
            local game_base_info = banner_model.get_game_base_info(action_info.game_id_);
            if game_base_info.sub_type == 30 then -- 免费单机游戏，不查询权限，仅查询白名单
                local game_lib = base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
                if nil ~= game_lib then
                    game_lib:GetRailGameReleaseStatus(action_info.game_id_,
                        "WGGameAction.on_get_rail_game_release_status");
                end
                return false;
            else -- 付费单机游戏，走游戏权限，游戏发布状态来查询是否可以下载
                action_info.action_id_ = action;
                
                local callback_func = rail_proxy_event.QUERY_GAME_INFO_ON_DOWNLOAD_GAME_FROM_BANNER_BTN;
                            
                local lua_table = { }
                lua_table.dest = "0";
                lua_table.seq_num = "0";
                lua_table.callback_func = callback_func;
                lua_table.data = { }
                lua_table.data.game_id = action_info.game_id_;
                lua_table.data.need_refund_info = 0;
                lua_table.src = "0";

                local rail_host = base_utility.get_component_adapter(
                    "IRailHost", "ierd_tgp.tgp_plugin.GetRailHostAdapter();");
                rail_host:QueryGameAuthorityInfo(base_utility.table_to_json(lua_table));
                return false;
            end
        end
    end
    
    -- 取消下载/更新时弹出提示
    if action == GameActions.kCancelDownload or 
        action == GameActions.kCancelUpdate then

        local desc_text = "";
        local desc_title_text = "";
        if action == GameActions.kCancelDownload then
            -- desc_text = LoadStr("CANCEL_DOWNLOAD_GAME_DESC");
            desc_title_text = LoadStr("CANCEL_DOWNLOAD_GAME_DESC_TITLE");
        else
            desc_text = LoadStr("CANCEL_UPDATE_GAME_DESC");
            desc_title_text = LoadStr("CANCEL_UPDATE_GAME_DESC_TITLE");
        end

        local param = {
            position = 'center',--参数值: rbottom-右下角，center-居中
            title = LoadStr("POP_TIPS_TIPS"),
            width = 480,
            height = 230,
            titleIcon = "",
            icon = "",
            tipsIcon = "",
            descTitle = desc_title_text,
            desc = desc_text,
            cancelBtn = {
                visible = true,
                text = LoadStr("CANCEL_UPDATE_CANCEL"),
                handle = function()
                end
            },
            okBtn = {
                visible = true;
                text = LoadStr("CANCEL_UPDATE_EXIT"),
                handle = function()
                    WGGameAction.execute_action_impl(action, action_info);
                    poptips_control.close_tips();
                end
            }
        }
        poptips_control.show_tips(param);
        return true;
    end

    WGGameAction.execute_action_impl(action, action_info);
    return true;
end

function WGGameAction.execute_action_impl(action, action_info)
    if action_info.zone_id_ == nil then
        base_utility.log("[Banner]excute_action zone_id_ is nil");
        action_info.zone_id_  = 0;
    end
    --[[
    base_utility.log(string.format("[Banner]excute_action[action:%d], game id:%d, zone id:%s, uid:%d",
        action, action_info.game_id_, action_info.zone_id_, action_info.uid_));
    ]]
    local func = WGGameAction.act2fun_[action];
    if func == nil then
        --[[
        base_utility.log(string.format("[Banner]action[action:%d] is NIL, gameid:%d, zone id:%s",
            action, action_info.game_id_, action_info.zone_id_), true);
        ]]
        return;
    end

    func(action_info)
end

function WGGameAction.on_query_game_authority_info(event_id, json_str)
    base_utility.log("[Banner]on_query_game_authority_info, json_str = "..json_str);
    local table_param = base_utility.json_to_table(json_str);
    if (table_param == nil) then
        return;
    end;

    local callback_func = table_param["callback_func"];
    if (callback_func == nil) then
        return;
    end;
    -- 只处理下载游戏时的查询权限
    if (callback_func ~= rail_proxy_event.QUERY_GAME_INFO_ON_DOWNLOAD_GAME_FROM_BANNER_BTN and
        callback_func ~= rail_proxy_event.QUERY_GAME_INFO_ON_DOWNLOAD_GAME) then
        return;
    end;

    if table_param.result == 0 and table_param.authority_info.result == 0 then
        if table_param.authority_info.state == 1 then  -- 1: 有权限
            -- 鉴权通过后再通过接口判断是否有下载权限
            local game_lib =
                base_utility.get_component_adapter("IGame_library",
                    "ierd_tgp.game_library.Get_game_library_adapter();");
            if nil ~= game_lib then
                game_lib:GetRailGameReleaseStatus(table_param.game_id,
                    "WGGameAction.on_get_rail_game_release_status");
            end;
        else
            WGGameAction.open_game_detail_page(table_param.game_id);
        end
    else
        -- 查询权限失败则弹框提示获取权限信息失败
        local param = {};
        param.error_code = table_param.result;
        if param.error_code ~= nil and param.error_code == 50 then  -- 50: 网络连接断开
            popup_wnd_model.show_switch_offline_mode_wnd(param);
        else
            popup_wnd_model.show_query_game_info_failed_wnd(param);
        end;
    end
end

function WGGameAction.OnQueryGameAuthorityInfoErrorHappened(event_id, json_param)
    base_utility.log("[Banner]OnQueryGameAuthorityInfoErrorHappened, json_str:"..json_param);

    local json_data = base_utility.json_to_table(json_param);
    if (json_data ~= nil and json_data.game_id ~= nil and
        json_data.error_type ~= nil and json_data.error_code_pb_protocal ~= nil) then
        local game_id = json_data.game_id;
        local error_type = json_data.error_type;
        local error_code_pb_protocal = json_data.error_code_pb_protocal;
        local error_msg = json_data.error_msg;
        local use_game_icon = true;
        local tips_icon = nil;
        if  use_game_icon == true and game_id ~=nil and 0 ~= tonumber(game_id) then
            popup_wnd_model.set_game_icon(tonumber(game_id));
            tips_icon = popup_wnd_model.icon_url;
        end
        if (error_type == ierd_tgp.game_library.ERROR_QUERY_GAME_AUTHORITY_FAILED) then
            -- 查询权限失败则弹框提示获取权限信息失败
            local param = {};
            param.tips_icon = tips_icon;
            param.error_code = error_code_pb_protocal;
            if error_code_pb_protocal == 50 then  -- 50: 网络连接断开
                popup_wnd_model.show_switch_offline_mode_wnd(param);
            else
                popup_wnd_model.show_query_game_info_failed_wnd(param);
            end;
        elseif (error_type == ierd_tgp.game_library.ERROR_WEGAME_EXCEPTION_MODE) then
            local param = {};
            param.tips_icon = tips_icon;
            popup_wnd_model.show_switch_offline_mode_wnd(param);
        elseif (error_type == ierd_tgp.game_library.ERROR_HAS_NO_GAME_AUTHORITY) then
            -- 没有权限
            WGGameAction.open_game_detail_page(game_id);
        elseif (error_type == ierd_tgp.game_library.ERROR_GAME_IS_REFUNDING) then
            -- 游戏退款中
            local param = { desc = LoadStr("WE_GAME_GAME_ACTION_STOP"),
                onOk = function() end,
                button_text = LoadStr("WE_GAME_GAME_ACTION_SAVE"),
                onCancel = function() end,
                tips_icon = tips_icon};
            popup_wnd_model.show_game_refunding_state_wnd(param);
        elseif (error_type == ierd_tgp.game_library.ERROR_DLC_IS_REFUNDING) then
            -- DLC退款中
            local param = { desc = LoadStr("WE_GAME_GAME_ACTION_LONG"),
                onOk = function() end,
                button_text = LoadStr("WE_GAME_GAME_ACTION_SAVE"),
                onCancel = function() end,
                tips_icon = tips_icon};
            popup_wnd_model.show_game_refunding_state_wnd(param);
        elseif (error_type == ierd_tgp.game_library.ERROR_AUTO_ACTIVE_AUTHORITY_CLOSE) then
            local req = {};
            req.game_id = game_id;

            local param = {tips_icon = tips_icon};
            local white_msg = comm_center.call_service("i_service_launch_game_white_list_msg",req);
            if white_msg == nil then
                param.error_msg = LoadStr("BANNER_ACTION_GAME_NOT_OPEN");
            else
                param.error_msg = white_msg.msg;
                param.rich_error_msg = white_msg.rich_msg;
            end
            
            base_utility.log("[Banner]OnQueryGameAuthorityInfoErrorHappened,"..param.error_msg);
            if param.error_msg == "" then
                param.error_msg = LoadStr("BANNER_ACTION_GAME_OVERLOAD");
            end
            popup_wnd_model.show_query_game_info_failed_wnd_2nd(param);
        elseif (error_type == ierd_tgp.game_library.ERROR_AUTO_ACTIVE_AUTHORITY_BUSY or 
            error_type == ierd_tgp.game_library.ERROR_QUERY_GAME_PERMISSION_BUSY) then
            base_utility.log("[Banner]OnQueryGameAuthorityInfoErrorHappened, Busy.");
            local param = {};
            param.tips_icon = tips_icon;
            param.error_msg = LoadStr("BANNER_ACTION_GAME_TOO_MANY_VISIT").. error_code_pb_protocal;
            popup_wnd_model.show_query_game_info_failed_wnd_2nd(param);
        elseif (error_type == ierd_tgp.game_library.ERROR_LOCK_AREA_ERROR) then
            local param = {};
            param.tips_icon = tips_icon;
            param.error_msg = LoadStr("WE_GAME_GAME_LOCK_AREA").. error_code_pb_protocal;
            popup_wnd_model.show_query_game_info_failed_wnd_2nd(param);
        elseif (error_type == ierd_tgp.game_library.ERROR_QUERY_GAME_PERMISSION_FAILED or 
            error_type == ierd_tgp.game_library.ERROR_QUERY_GAME_PERMISSION_INVALID_INPUT) then
            local param = {};
            param.tips_icon = tips_icon;
            param.error_msg = error_msg;
            param.error_code = error_code_pb_protocal;
            if error_code_pb_protocal == 1000007 or error_code_pb_protocal == 1000010 then 
                popup_wnd_model.show_switch_offline_mode_wnd(param);
            else
                popup_wnd_model.show_query_game_info_failed_wnd(param);
            end;
        end;
    end;
end;

function WGGameAction.OnShowDownloadGame(event_id, json_param)
 base_utility.log("[Banner]OnShowDownloadGame, json_str:"..json_param);

    local json_data = base_utility.json_to_table(json_param);
    if json_data ~= nil and json_data.game_id ~= nil then
        local error_msg = LoadStr("POP_WND_GAME_NOT_INSTALLED");
        local param = {
            position = "center",
            title = LoadStr("POP_WND_LAUNCH_DELETE_BRANCH_TITLE"),
            width = 480,
            height = 230,
            titleIcon = "",
            icon = "",
            tipsIcon = "",
            descTitle = "",
            desc = error_msg,
            okBtn = {
                visible = true,
                text = LoadStr("POP_WND_GAME_NOT_INSTALLED_OK"),
                handle = function()
                    local param = {};
                    param.type = "jump_url";
                    param.game_id = json_data.game_id;
                    param.url = LoadStr("COMMENT_INVITE_GAME_DETAIL")
                        .. tostring(json_data.game_id);
                    param.op_from = 0;
                    local json_str = base_utility.table_to_json(param);
                    web_utility.open_link(json_str);
                    poptips_control.close_tips();
                end,
            },
            cancelBtn = {
                visible = true,
                text = LoadStr("POP_WND_GAME_NOT_INSTALLED_CANCEL"),
                handle = function()
                end,
            }
        }
        ui_utility.show_tips(param);
    end;
end;


function WGGameAction.on_get_rail_game_release_status(event_id, json_param)
    if nil ~= json_param then
        base_utility.log("[Banner]on_get_rail_game_release_status, json_str:"..json_param);
        local json_data = base_utility.json_to_table(json_param);
        if nil ~= json_data then
            local tast_check = json_data["task_check"];
            local is_release = json_data["is_release"];
            local game_id = json_data["game_id"];
            if nil ~= tast_check and 
                "WGGameAction.on_get_rail_game_release_status" == tast_check then
                if nil ~= is_release and nil ~= game_id then
                    if true == is_release then
                        local game_lib =
                            base_utility.get_component_adapter("IGame_library",
                                "ierd_tgp.game_library.Get_game_library_adapter();");
                        if nil == game_lib then
                            base_utility.log("[Banner]on_get_rail_game_release_status, game_lib is nil");
                            return;
                        end
                        game_lib:set_promote_record(game_id, true);
                        
                        local action_info = RailGameActionInfo[tostring(game_id)];
                        if action_info ~= nil and action_info.branch_id_ ~= nil then
                            WGGameAction.download(action_info);
                        else
                            local json_str = base_utility.table_to_json({ game_id = game_id });
                            event_center.send_event(branch_selector_event.OPEN_GAME_BRANCH_SELECTOR, json_str);
                        end;
                    else
                        local param = {
                            position = 'center',  --参数值: rbottom-右下角，center-居中
                            title = LoadStr("BANNER_ACTION_INFO_TITLE"),
                            width = 480,
                            height = 230,
                            titleIcon = "",  --参数值: ONEIMAGE:login\\base\\2\\login_bg.jpg
                            icon = "",
                            tipsIcon = "",
                            descTitle = "",
                            desc = LoadStr("BANNER_ACTION_DOWNLOAD_NOT_OPEN"),
                            cancelBtn = {
                                visible = false,
                                text = "",
                                handle = function()
                                end
                            },
                            okBtn = {
                                visible = true;
                                text = LoadStr("BANNER_ACTION_INFO_OK"),
                                handle = function()
                                    poptips_control.close_tips();
                                end
                            }
                        }
                        poptips_control.show_tips(param);
                    end
                end
            end
            RailGameActionInfo[tostring(game_id)] = nil;
        end
    end
end;

function WGGameAction.open_game_detail_page(game_id)
    local param = {};
    param.type = "jump_url";
    -- param.sub_page = "GAME_DETAIL";
    param.game_id = game_id;
    param.url = LoadStr("COMMENT_INVITE_GAME_DETAIL")
        .. tostring(game_id) .."&no_permission=1";
    param.op_from = 0;
    local json_str = base_utility.table_to_json(param);
    web_utility.open_link(json_str);
end

function WGGameAction.download(action_info)
    base_utility.log(string.format("[Banner]downloading game"));

    local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                        {game_id = action_info.game_id_});
    if data_out ~= nil and not data_out.bAllowContinue then
        return
    end
    -- 调用业务接口
    local branch_id = 0;
    if action_info.branch_id_ ~= nil then
        branch_id = tonumber(action_info.branch_id_);
    end;
    local param = {};
    param["game_id"] = action_info.game_id_;
    param["dest_branch_id"] = branch_id;
    comm_center.call_service("Svr_DownloadGame", param);
    if banner_model.is_game_in_subscription_list(action_info.game_id_) then
        banner_control.banner_report(banner_qos_action.kActionDownloadSubscribed, action_info.game_id_);
    end
end

function WGGameAction.launch(action_info)
    base_utility.log(string.format("[Banner]launching game"));

    -- 调用业务接口
    if action_info.game_id_ == GAME_ID.GAME_ID_MC then
        if WGGameAction.PopTipsForInstallMC() == true then
            return;
        end
    end
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    local op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND;
    if nil ~= action_info.launch_op_from and 0 ~= action_info.launch_op_from then
        op_from = action_info.launch_op_from;
    end
    game_lib:launch_game(action_info.game_id_, action_info.zone_id_,
        action_info.uid_, op_from, "3100081-1",
        ierd_tgp.game_library.ZONE_TYPE_FORMAL, action_info.version_);
end

function WGGameAction.update(action_info)
    base_utility.log(string.format("[Banner]update game"));
    -- 弹框提示用户
    local json_str = base_utility.table_to_json(action_info);
    local server = banner_model.get_server_by_zoneid(action_info.game_id_, action_info.zone_id_);
    -- C++端pre_update用的是true而不是TRUE
    if server ~= nil and server.pre_update == true 
        and ierd_tgp.game_library.LAUNCH_OP_FROM_LACK_SPACE_TIPS ~= action_info.launch_op_from then
        event_center.send_event(popup_wnd_event.PRE_UPDATE_GAME, json_str);
    else
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND;
        if nil ~= action_info.launch_op_from and 0 ~= action_info.launch_op_from then
            op_from = action_info.launch_op_from;
        end
        local data_out = comm_center.call_service("i_service_allow_continue_operation_game",
                            {game_id = action_info.game_id_});
        if data_out ~= nil and not data_out.bAllowContinue then
            return
        end
        game_lib:update_game(action_info.game_id_, action_info.zone_id_, op_from);
        event_center.send_event(main_event.UPDATE_GAME_START, "");
    end;
end

function WGGameAction.install(action_info)
    base_utility.log(string.format("[Banner]install game"));
    -- 调用业务接口
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:install_game(action_info.game_id_);
end

function WGGameAction.pause_download(action_info)
    base_utility.log(string.format("[Banner]pause_download"));
    -- 调用业务接口
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:pause_download_game(action_info.game_id_);
              
    local run_mode = game_lib:GetRunMode();

    MainCommon.ReportQosMainBtnClick(QosMainBtnId.kPauseDownload, action_info.game_id_, run_mode);
end

function WGGameAction.continue_download(action_info)
    base_utility.log(string.format("[Banner]continue_download"));
    -- 调用业务接口
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:continue_download_game(action_info.game_id_);
       
    local run_mode = game_lib:GetRunMode();
     
    MainCommon.ReportQosMainBtnClick(QosMainBtnId.kContinueDownload, action_info.game_id_, run_mode);
end

function WGGameAction.pause_update(action_info)
    base_utility.log(string.format("[Banner]pause_update"));
    -- 调用业务接口
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:pause_update_game(action_info.game_id_);
end

function WGGameAction.cancel_download(action_info)
    base_utility.log(string.format("[Banner]cancel_download"));
    -- 调用业务接口
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:cancel_download_game(action_info.game_id_);

    if banner_model.is_game_in_subscription_list(action_info.game_id_) then
        banner_model.set_game_cancel_download(action_info.game_id_);
    end

end

function WGGameAction.stop_game(action_info)
    base_utility.log(string.format("[Banner]stop_game"));
    -- 调用业务接口
    --local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    --game_lib:stop_game(action_info.game_id_);
    
    -- 弹框提示用户
    local json_str = base_utility.table_to_json(action_info);
    event_center.send_event(popup_wnd_event.EXIT_GAME, json_str);
end

function WGGameAction.cancel_update(action_info)
    base_utility.log(string.format("[Banner]cancel_update"));
    -- 调用业务接口
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    game_lib:cancel_update(action_info.game_id_);
                           
    local run_mode = game_lib:GetRunMode();
    MainCommon.ReportQosMainBtnClick(QosMainBtnId.kCancelUpdate, action_info.game_id_, run_mode);
end

function WGGameAction.update_plugin(action_info)
    base_utility.log(string.format("[Banner]update_plugin"));
    local plugin_mgr =
        base_utility.get_component_adapter("IPlugin_mgr",
            "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
    plugin_mgr:UpdatePlugin(action_info.game_id_);
end

function WGGameAction.pause_update_plugin(action_info)
    base_utility.log(string.format("[Banner]pause_update_plugin"));
    local plugin_mgr =
        base_utility.get_component_adapter("IPlugin_mgr",
            "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
    plugin_mgr:CancelUpdate(action_info.game_id_);
end

function WGGameAction.cancel_update_plugin(action_info)
    base_utility.log(string.format("[Banner]cancel_update_plugin"));
    local plugin_mgr =
        base_utility.get_component_adapter("IPlugin_mgr",
            "ierd_tgp.tgp_plugin.GetPluginMgrModule();");
    plugin_mgr:CancelUpdate(action_info.game_id_);
end

function WGGameAction.PopTipsForInstallMC()
    local servers = banner_model.get_game_servers(GAME_ID.GAME_ID_MC);
    if servers ~= nil and servers.server_count ~= 0 then
        return false;
    end
    local param = {
        position = 'center',--参数值: rbottom-右下角，center-居中
        title = LoadStr("WE_GAME_GAME_ACTION_TIPS"),
        width = 480,
        height = 230,
        titleIcon = "",--参数值: ONEIMAGE:login\\base\\2\\login_bg.jpg
        icon = "",
        tipsIcon = "",
        descTitle = "",
        desc = LoadStr("WE_GAME_GAME_ACTION_MC"),
        cancelBtn = {
            visible = false,
            text = "",
            handle = function()
            end
        },
        okBtn = {
            visible = true;
            text = LoadStr("WE_GAME_BANNER_BANNER_OK"),
            handle = function()
                local table_str = {};
                table_str.type = "landing";
                table_str.game_id = GAME_ID.GAME_ID_MC;
                table_str.close_origin = false;
                local json_str = base_utility.table_to_json(table_str);
                web_utility.open_link(json_str);
                poptips_control.close_tips();
            end
        }
    }
    poptips_control.show_tips(param);
    return true;
end

-- 根据登录类型检查游戏启动权限
function WGGameAction.IsFilterGameInAccLaunchCfg(game_id)
    if TLoginModule.GetLoginAccountType() == TLoginModule.LOGIN_ACCOUNT_TYPE.kQQLogin then
        return false;
    elseif TLoginModule.GetLoginAccountType() == TLoginModule.LOGIN_ACCOUNT_TYPE.KWeChatLogin then
        return WGGameAction.IsGameIdInBlackList(game_id);
    else
        return WGGameAction.IsFilterInOtherAccountType(game_id);
    end;
end

-- Facebook和邮箱登录需要检查的配置
function WGGameAction.IsFilterInOtherAccountType(game_id)
    local is_filter = false;
    
    -- 默认所有端游不支持，所有单机支持
    local game_lib = base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
    if game_lib ~= nil then
        local channel = game_lib:get_game_distribution_channels(game_id);
        if banner_model.distribution_channels.CHANNELS_RAIL ~= channel then
            is_filter = true;
        else
            is_filter = false;
        end;
    else
        TGPLog.LogE("IsFilterInOtherAccountType", "get glib module failed");
    end;
    
    -- 基本前提下，白名单游戏支持，黑名单游戏不支持
    local data = {key = "game_launch_cfg_in_global_acc_type"};
    local nData = comm_center.call_service("i_svr_get_common_cfg", data);
    local cfg_json = "";
    if nil ~= nData and nData.value ~= nil and nData.value ~= "" then
        cfg_json = nData.value;
    else
        TGPLog.LogE("IsFilterInOtherAccountType", "query fail.");
    end;

    if cfg_json == "" then
        return is_filter;
    end;
    local cfg_info = base_utility.json_to_table(cfg_json);
    if cfg_info == nil then
        return is_filter;
    end;

    local acc_type = TLoginModule.GetLoginAccountType();
    for index, item in ipairs(cfg_info) do
        if item ~= nil and item.acc_type ~= nil and tonumber(item.acc_type) == acc_type then
            if item.white_list ~= nil then
                for w_index, w_item in ipairs(item.white_list) do
                    if w_item ~= nil and tonumber(w_item) ~= 0 and tonumber(w_item) == tonumber(game_id) then
                        is_filter = false;
                        break;
                    end;
                end;
            end;
            if item.black_list ~= nil then
                for b_index, b_item in ipairs(item.black_list) do
                    if b_item ~= nil and tonumber(b_item) ~= 0 and tonumber(b_item) == tonumber(game_id) then
                        is_filter = true;
                        break;
                    end;
                end;
            end;
        end;
    end;
    return is_filter;
end

function WGGameAction.IsGameIdInBlackList(game_id)
    local data = {key = "gameid_not_support_wechatlogin"};
    local   nData = comm_center.call_service("i_svr_get_common_cfg", data);
    local gameidlist = "none";
    if nil ~= nData and nData.value ~= nil and nData.value ~= "" then
        gameidlist = nData.value;
    else
        TGPLog.LogE("IsGameIdInBlackList", "query fail.")
    end;
    -- 网游根据oss配置来决定是否支持
    local game_lib =
                    base_utility.get_component_adapter("IGame_library",
                        "ierd_tgp.game_library.Get_game_library_adapter();");
    local channel = game_lib:get_game_distribution_channels(game_id);
    if banner_model.distribution_channels.CHANNELS_RAIL ~= channel then
        TGPLog.LogE("IsGameIdInBlackList", "is a net game");
        local game_info = banner_model.get_game_base_info(game_id);
        if game_info ~= nil and game_info.support_wechat_launch == 1 then
            TGPLog.LogE("IsGameIdInBlackList", "support_wechat_launch");
            return false;
        end
        TGPLog.LogE("IsGameIdInBlackList", "not support_wechat_launch");
        return true;
    else
        TGPLog.LogE("IsGameIdInBlackList", "wechatblacklist gameid:"..gameidlist);
        if string.find(gameidlist, tostring(game_id)) ~= nil then
            TGPLog.LogE("IsGameIdInBlackList", "gameid is in list..");
            return true;
        end
        return false;
    end   
end

function WGGameAction.PopTipsForSwitchQQLogin()
 --[[   
    local   sTipMsg = LoadStr("WEGAME_BANNER_NOT_SUPPORT_WECHAT");

    if TLoginModule.GetLoginAccountType() == TLoginModule.LOGIN_ACCOUNT_TYPE.KFacebookLogin then
        sTipMsg = LoadStr("FACBOOK_BANNER_NOT_SUPPORT_FACEBOOK");
    end;
]]--
    local param = 
    {
        position = "center",
        title = LoadStr("WEGAME_BANNER_INFO_TITLE"),
        width = 460,
        height = 230,
        titleIcon = "",
        tipsIcon = poptips_icons.kInfo,
        descTitle = LoadStr("LOGIN_ACCOUNT_TYPE_NOT_SUPPORT_LAUNCH_TITLE"),
        desc = LoadStr("LOGIN_ACCOUNT_TYPE_NOT_SUPPORT_LAUNCH"),
        okBtn = 
        {
            visible = "true",
            text = LoadStr("WEGAME_BANNER_QQ_LOGIN"),
            handle =  function()
                local gamelib = base_utility.get_component_adapter("IGame_library",
                                    "ierd_tgp.game_library.Get_game_library_adapter();");
                gamelib:do_switch_user();
                poptips_control.close_tips();
            end,
        },
        cancelBtn = 
        {
            visible = "true",
            text = LoadStr("WEGAME_BANNER_INFO_CANCEL"),
            handle = function()
            end,
        },
    };

    ui_utility.show_tips(param);
end

WGGameAction.init();
